/**
 * Copyright 2011-2013 Brin.com
 * All rights reserved.
 * 
 * @project
 * @author Brin
 * @version 1.0
 * @date 2012-09-29
 */
package com.brin.code.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.brin.core.common.CookieManager;
import com.brin.core.util.GlobEnv;
import com.brin.core.util.StringUtil;

/**
 * @description 当用户未登录或页面错误时，跳转首页
 * @author Brin
 *
 */
public class GlobalInterceptor extends HandlerInterceptorAdapter{

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception{
		CookieManager cookie = new CookieManager(request, response);
		String user = cookie.getCookieValue("_user");
		String role = cookie.getCookieValue("_role");
		String id = cookie.getCookieValue("_id");
		
		if(StringUtil.isBlank(user) || StringUtil.isBlank(role) || StringUtil.isBlank(id)){
			String temp[] = request.getRequestURL().toString().split("/");
			if("demo".equals(temp[4])){
				return true;
			}else{
				response.sendRedirect(GlobEnv.getWebURL(""));
				return false;
			}
		}
		
//		if (userService.isUser(URLDecoder.decode(_user, "UTF-8"), null) == null) {
//			response.sendRedirect(GlobEnv.getWebURL("/login/index.htm"));
//			
//			return false;
//		}
		
		return true;
	}
}
